import tornado.web
import tornado.ioloop
from . import utils
import json
'''
#
#6.8 查询与指定人脸相似的人脸记录
同步
    http://ip:port/face/faceSearch
    {
        "dbName":"test",
        "fileUrl":"",
        "id":"1",
        "topNum":2,
        "score":0.3
    }

'''
class FaceSearch(tornado.web.RequestHandler):

    def post(self, *args, **kwargs):
        resp = None
        try:
            data=json.loads(self.request.body)
            print(data)
            paramerror = utils.checkParam(data,{
                "dbName":"test",
                "fileUrl":"",
                "id":"1",
                "topNum":2,
                "score":0.3
            })
            if paramerror == '': #ok
                resp = {
                    "state":{
                        "ok":1,
                        "failed": ""
                    },
                    "result":"[{\"score\":0.99999017,\"imageId\":\"bcesadfaef-aefa-efawef--aefafeaf-aef\"}]",
                    "id":data['id']
                }
            else: #failed
                resp = {
                    "state":{
                        "ok":0,
                        "failed": paramerror
                    },
                    "result":""
                }

        except BaseException as e:
            resp = {
                "state":{
                    "ok":0,
                    "failed": str(e)
                },
                "result":""
            }

        self.write(resp)
